package cn.web.tunnelrobot.service.impl;

import cn.web.tunnelrobot.dao.RealTimeDataDao;
import cn.web.tunnelrobot.dto.RealTimeData;
import cn.web.tunnelrobot.service.RealTimeDataService;
import cn.web.tunnelrobot.util.Constants;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class RealTimeDataServiceImpl implements RealTimeDataService {

    @Resource
    private RealTimeDataDao historyDataDao;

    @Override
    public void saveHitoryData(RealTimeData realTimeData) {
        historyDataDao.saveHitoryData(realTimeData);
    }

    @Override
    public HashMap<String, Object> HistoryDatalist(Integer page, Integer size) {
      if (page == null){
          page=1;
      }
      if(size==null){
          size=10;
      }
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,size);
      List<RealTimeData> list = historyDataDao.findAll();
      if(list.isEmpty()){
          map.put("code",-1);
          map.put("msg","未查询到数据");
      }
      else{
          PageInfo historydatalist = new PageInfo(list);
          map.put("code",0);
          map.put("data",historydatalist);
      }
      return map;

//    @Overrid
//    public HistoryDataUtil<RealTimeData> list(RealTimeData re。。。。。。。。。。。。。。。。。。。。。。。alTimeData) {
//    RealTimeData list = historyDataDao.list(realTimeData);
//    return HistoryDataUtil.success(list);
    }

    @Override
    public HashMap<String, Object>  getHistoryDataByTime(
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
                    String time) {
        System.out.println(time);
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(1,10);
        List<RealTimeData> list = historyDataDao.getHistoryDataByTime(time);
        if (list.isEmpty()){
            map.put("code",-1);
            map.put("msg","未查询到数据");
        }
        else {
            PageInfo historydatatimelist = new PageInfo(list);
            map.put("code",0);
            map.put("data",historydatatimelist);
        }
        return map;
    }

    public HashMap<String,Object> getChargingsState(){
        HashMap<String, Object> map = new HashMap<>();
        String charge = Constants.doGet(Constants.chargingsState);
        JSONObject jsonObject = JSON.parseObject(charge);
        JSONObject datalist = (JSONObject) jsonObject.get("data");
        Object o = datalist.get("chargeStatus");
        int dianchistate = Integer.parseInt(o.toString());
        switch (dianchistate){
            case 0:
                map.put("code",0);
                map.put("msg","未充电");
                break;
            case 1:
                map.put("code",0);
                map.put("msg","充电中");
                break;
            case 2:
                map.put("code",0);
                map.put("msg","已充满，但仍在小电流充电");
                break;
            case 3:
                map.put("code",0);
                map.put("msg","寻找充电桩");
                break;
            case 4:
                map.put("code",0);
                map.put("msg","停靠充电桩");
                break;
            default:
                map.put("code",-1);
                map.put("msg","充电状态异常，请检查硬件");
                break;
        }
        return map;
    }

}
